package com.zzyl.common.ai;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ResponseFormatJsonObject;
import com.openai.models.chat.completions.ChatCompletion;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AIModelInvoker {
    @Autowired
    private BaiduAIProperties baiduAIproperties;
    public String Invoke(String prompt) {
        log.info("调用百度千帆大模型，分析体检报告，获取分析结果");
        log.info("prompt:{}",prompt);
        log.info("apiKey:{}",baiduAIproperties.getApiKey());
        log.info("baseUrl:{}",baiduAIproperties.getBaseUrl());
        OpenAIClient openAIClient = OpenAIOkHttpClient.builder()
                .apiKey(baiduAIproperties.getApiKey())
                .baseUrl(baiduAIproperties.getBaseUrl())
                .build();
        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage(prompt)
                .model(baiduAIproperties.getModel())
                .responseFormat(ChatCompletionCreateParams.ResponseFormat.ofJsonObject(ResponseFormatJsonObject.builder().build()))
                .build();
        ChatCompletion chatCompletion = openAIClient.chat().completions().create(params);

        return chatCompletion.choices().get(0).message().content().orElseGet(() -> "");
    }
}
